<?php
/**
 * Controller_Zags
 *
 * Класс Controller_Zags отвечает за обработку страниц, связанных с ЗАГСами
 *
 * @author Дмитрий Кузьма <kouzma.d@gmail.com>
 * @version 1.0
 */
class Controller_Zags extends Controller_Base {

	/**
	 * Выбор действия по URL
	 *
	 */
	public function action() {
	  Service_Debug::setDebug();
	  if (!$this->cityUrl) {
	    $this->addCrumb('ЗАГСы', W_ABSPATH . 'zags/');
	  }
	  else {
	  	$objCityUrl = new CityUrl;
      $cityRod = Service_Declination::getGenitive($this->cityName);
      $this->addCrumb('ЗАГСы ' . $cityRod, W_ABSPATH . $this->city . '/zags/');
	  }
		switch ($this->arrRequest[1]) {
			case '':
				if (!$this->cityUrl) $this->actionIndex();
				else $this->actionCityIndex();
				break;
			case 'add':
				if (!SimpleAdministration::isAdmin()) $this->setError(403);
				$this->actionAdd();
				break;
			case 'edit':
				if (!is_numeric($this->arrRequest[2])) $this->setError(404);
				else { 
					$objZags = new Zags($this->arrRequest[2]);
					if ($objZags->id) {
						if (!SimpleAdministration::isAuthorOrAdmin($objZags->getField('ID_user'), true)) $this->setError(403);
						$this->actionEdit($objZags);
					}
					else $this->setError(404);
				}
				break;
			default:
				$objZags = new Zags();
				$objZags->find('first', array(
				  'conditions' => "
				    `city` = '" . $this->city . "' AND `zags_alias` = '" . $this->arrRequest[1] . "'
				  "
				));
				
				if ($objZags->id) {
					echo $this->onezags($objZags);
				} else $this->setError(404);
				break;
		}
		
	}
	
  /**
   * Выводит список городов
   * 
   */
  public function actionIndex() {
    $objCityUrl = new CityUrl();
    $arrCitiesWithCountries = $objCityUrl->getCitiesWithCountries();
    
  	$this->title = 'Загсы России и СНГ. Загсы и дворцы бракосочетания Москвы, Санкт-Петербурга, Нижнего Новогорода, Ростова-на-Дону, Киева и др.';
    $this->pageHead = 'Загсы России и СНГ';
    
  	$this->showHeader();
      View_Zags::showCitiesList($arrCitiesWithCountries);
    $this->showFooter();
  }
  
 	
  /**
   * Выводит страницу добавления ЗАГСа
   * 
   */
  public function actionAdd() {
  	if (isset($_POST['Zags'])) {
			$objZags = new Zags($_POST['Zags']);
			if ($objZags->save()) User_Session::moveTo(W_ABSPATH . $objZags->getField('city') . '/zags/', 'Загс успешно добавлен', 1);
		}
		else $objZags = new Zags();
		$objCityUrl = new CityUrl();
		$arrCities = $objCityUrl->getCitiesForSelectField();
  	
  	$objViewZags = new View_Zags();
  	$this->pageHead = $this->title = 'Добавление ЗАГСа';
    $this->addWysiwygMetas();
    $this->addCrumb('Добавление ЗАГСа');
    
  	$this->showHeader();
      $objViewZags->showEditForm($objZags, $arrCities);
    $this->showFooter();
  }
  
 	
  /**
   * Выводит страницу редактирования ЗАГСа
   * 
   * @param Zags $objZags - объект ЗАГСа для редактирования
   */
  public function actionEdit(Zags $objZags) {
		if (isset($_POST['Zags'])) {
			$objZags = new Zags($_POST['Zags']);
			if ($objZags->save()) User_Session::moveTo(W_ABSPATH . $objZags->getField('city') . '/zags/', 'Загс успешно добавлен', 1);
		}
		$objCityUrl = new CityUrl();
		$arrCities = $objCityUrl->getCitiesForSelectField();
  	
  	$objViewZags = new View_Zags();
  	$this->pageHead = $this->title = 'Редактирование ЗАГСа "' . $objZags->getField('zags_name') . '"';
  	$this->addUrl = W_ABSPATH . 'zags/add/';
  	$this->addWysiwygMetas();
    
  	$this->showHeader();
      $objViewZags->showEditForm($objZags, $arrCities);
    $this->showFooter();
  }
  
  
	
  /**
   * Главная страница ЗАГСов конкретного города
   * 
   */
	public function actionCityIndex() {
    $objCityUrl = new CityUrl;
    $cityRod = Service_Declination::getGenitive($this->cityName);
    $objZags = new Zags();
    $arrZagses = $objZags->find('all', array('conditions' => "`city` = '" . $this->city . "'", 'order' => '`zags_name` ASC'));
    $objUser = new User_User();
    $arrUsers = $objUser->find('all', array());
    
    $objDB = Service_Database::instance();
    
    
    $objViewZags = new View_Zags();
		$this->pageHead = $this->title = 'Загсы ' . $cityRod;
		$this->addUrl = W_ABSPATH . $this->city . '/zags/add/';
		$this->addCss('my.css');
  	
		$this->showHeader();
  	  $objViewZags->showList($arrZagses);
  	  echo '<pre>';
  	  	print_r($arrZagses);
  	  echo '</pre>';
    $this->showFooter();
	}
	
	public function onezags(Zags $objZags) {
		$this->showHeader();
			echo '<pre>';
				print_r($objZags->tableFields);
			echo '</pre>';
			echo $objZags->tableFields['zags_name'];
			echo '<br />';
			echo $objZags->getField('zags_name');
    $this->showFooter();
	}
}
?>